<?php

namespace app\common\service\auth;

use app\model\Agent;
use think\facade\Db;

class AuthAgentService extends AuthBase implements AuthInterface
{

    /**
     * @var Agent $agent
     */
    public $agent;

    public function attempt($params)
    {

        $accessToken = request()->header('Authorization');
        if (empty($accessToken)) {
            return false;
        }
        $agentInfo = cache($accessToken);
        if (empty($agentInfo)) {
            return false;
        }

        $agent = Agent::query()
            ->where('id', $agentInfo['agent_id'] ?? 0)
            ->where('is_deleted', 0)
            ->find();

        if (empty($agent)) {
            return false;
        }
        if ($agent['status'] != 1) {
            return false;
        }
        $auth = new \app\agent\model\Auth;
        $auth->refreshToken($agentInfo['refresh_token']);

        $this->agent = $agent;

        return $agent;
    }

    public function login($params)
    {
        //老代码实现
    }

    public function id()
    {
        return $this->agent->id;
    }

    public function user()
    {
        //怕以后有代理员工管理，这个非这个场景一定不能用
    }

    public function agentId()
    {
        return $this->agent->id;
    }


}